let express = require('express');
let router = express.Router();
const { updateSql, insertSql, deleteSql } = require('../../utils');
let connection = require('../dataBase');
const Result = require('../result');

router.post('/add', function (req, res) {
  const sql = insertSql('attention', req.body);
  connection.query(sql, (err, data) => {
    if (err) {
      return new Result(res, 1200).fail();
    }
    return new Result(res, 200, data, '添加成功').success();
  });
});

router.post('/delete', function (req, res) {
  const sql = deleteSql('attention', req.query.attentionId);
  connection.query(sql, (err, data) => {
    if (err) {
      return new Result(res, 1200).fail();
    }
    return new Result(res, 200, data, '添加成功').success();
  });
});

router.get('/getAll', function (req, res) {
  const sql = `SELECT acc.id,acc.avatar,acc.nickName from attention, account acc where accountId = ${req.query.accountId} and attention.otherAccountId=acc.id`;
  connection.query(sql, (err, data) => {
    if (err) {
      return new Result(res, 1200).fail();
    }
    return new Result(res, 200, data, '添加成功').success();
  });
});

module.exports = router;
